// payment.js
var api = require('../../utils/api');
var wxpay = require('../../utils/wxpay');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchOrder: {
      orderNo: "",
      orderStatus: [],
      createId: "0",
      id: ""
    },
    hiddenmodalinput: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderNo = options.orderNo;
    this.data.searchOrder.orderNo = orderNo;
    var createId = app.globalData.opendId;
    this.data.searchOrder.createId = createId;
    
    this.getOrderDetail();
  },
  getOrderDetail: function () {

    var url = api.getOrderDetailUrl();
    var params = this.data.searchOrder;
    var that = this;
    api.request(url, 'POST', params, function (res, err) {
      that.setData({ order_detail: res });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
   tapName: function () {
     
     var orderNo=this.data.searchOrder.orderNo ;
     var createId = app.globalData.opendId;
     var id = this.data.order_detail.id;
     var params = {
       "createId": createId,
       "orderId": id,
       "orderNo": "",
       "totalPrice": ""
     };
     var url = api.payOrder();
     var that = this;
     api.request(url, 'POST', params, function (res, err) {
     debugger
       var return_code = res.return_code;
       var result_code = res.result_code;
       if (return_code == 'SUCCESS' && result_code == 'SUCCESS') {
       var timeStamp = res.timeStamp;
       var nonceStr = res.nonce_str;
       var prepay_id = "prepay_id=" + res.prepay_id;
       var signType = "MD5";
       var paySign = res.paySign;
       var payParams = {
         timeStamp: timeStamp,
         nonceStr: nonceStr,
         "package": prepay_id,
         signType: signType,
         paySign: paySign
       };

       wxpay.requestPayment(payParams, that.paySuccess, that.payFail, that.payComplete);
       }else{
        wx.showToast({
          title: res.err_code_des,
          icon: 'succes',
          duration: 1000,
          mask: true
        });
        return;
       }
     });
  }, 
   paySuccess: function () {
     var totalPrice = this.data.order_detail.totalPrice;
     var orderNo = this.data.order_detail.orderNo;
     wxpay.payCallback(orderNo);
     wx.navigateTo({
       url: '../paymentresult/payment_result?totalPrice=' + totalPrice + "&orderNo=" + orderNo
     })
   }, payFail: function () {

   }, payComplete: function () {

   },
   modalinput:function(e){
    var url = api.getTimeTable();
    var that = this;
    api.request(url, 'GET', { 'staId': e.currentTarget.dataset.statid }, function (res, err) {
      that.setData({
        hiddenmodalinput: !that.data.hiddenmodalinput,
        timeTableList: res
      })
    });
 
  }, skcancel: function () {
    this.setData({
      hiddenmodalinput: true
    })
  },
  skconfirm: function () {
    this.setData({
      hiddenmodalinput: !this.data.hiddenmodalinput
    })
  },
  cancelOrder:function(){
    var url = api.cancelOrder();
    var id = this.data.order_detail.id;
    
    api.request(url, 'POST', { 'id': id }, function (res, err) {
     wx.switchTab({
       url: '../order/order_list',
       success: function (e) {
         var page = getCurrentPages().pop();
         if (page == undefined || page == null) return;
         page.onLoad();  
       }
     });
    });
  
  }
})
